﻿using System;
using System.Linq;
using System.Windows.Forms;
using ReportWizard.Entities;

namespace ReportWizard.Controls
{
    public partial class PhotoListThumbnails : UserControl
    {
        public ReportImageFileList PhotoSet { get; set; }

        const float ControlSide = 240F;

        public PhotoListThumbnails(ReportImageFileList photoSet)
        {
            InitializeComponent();

            PhotoSet = photoSet;
        }

        private void FillPhotoList()
        {
            if (PhotoSet != null)
            {
                const int columnCount = 3;
                int rowCount =
                    Convert.ToInt32(Math.Ceiling(Convert.ToDouble(PhotoSet.Photos.Count)/Convert.ToDouble(columnCount)));

                TableLayoutPanel tlpThumbnails = new TableLayoutPanel
                                                     {
                                                         ColumnCount = columnCount,
                                                         RowCount = rowCount,
                                                         Dock = DockStyle.Top,
                                                         Height =
                                                             Convert.ToInt32((rowCount*ControlSide) + (rowCount*10))
                                                     };
                tlpThumbnails.SuspendLayout();

                int placedControls = 0;
                bool thumbsPlaced = false;

                for (int i = 0; i < rowCount; i++)
                {
                    if (!thumbsPlaced)
                    {
                        if (tlpThumbnails.RowStyles.Count == i + 1)
                        {
                            tlpThumbnails.RowStyles[i] = new RowStyle(SizeType.Absolute, ControlSide);
                        }
                        else
                        {
                            tlpThumbnails.RowStyles.Add(new RowStyle(SizeType.Absolute, ControlSide));
                        }

                        for (int j = 0; j < columnCount; j++)
                        {
                            if (placedControls == PhotoSet.Photos.Count)
                                thumbsPlaced = true;

                            if (!thumbsPlaced)
                            {
                                PhotoInfo pi = new PhotoInfo(PhotoSet.Photos[placedControls])
                                                    {
                                                        Margin = new Padding(5)
                                                    };
                                pi.Click += ImageThumbClick;

                                if (tlpThumbnails.ColumnStyles.Count == j + 1)
                                {
                                    tlpThumbnails.ColumnStyles[j] = new ColumnStyle(SizeType.Absolute, ControlSide);
                                }
                                else
                                {
                                    tlpThumbnails.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, ControlSide));
                                }

                                tlpThumbnails.Controls.Add(pi, j, i);

                                placedControls++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                pThumbnails.Controls.Add(tlpThumbnails);

                pThumbnails.Height = tlpThumbnails.Height;
                Height = pThumbnails.Height;

                pThumbnails.Width = tlpThumbnails.Width;
                Width = pThumbnails.Width;

                tlpThumbnails.ResumeLayout();
            }
        }

        private void ImageThumbClick(object sender, EventArgs e)
        {
            PhotoInfo pi = (PhotoInfo)sender;
            if (pi.Parent != null && pi.Parent is TableLayoutPanel)
            {
                TableLayoutPanel parentPanel = (TableLayoutPanel)pi.Parent;
                TableLayoutPanelCellPosition tlpcp = parentPanel.GetCellPosition(pi);

                int[] denidedRows = new int[] { -1, 0, parentPanel.RowCount - 1 };

                if (tlpcp.Column != -1 && tlpcp.Column != -1)
                {
                    int rowCount = parentPanel.RowCount;
                    int columnCount = parentPanel.ColumnCount;


                    if (denidedRows.Contains(rowCount))
                    {

                    }
                }
            }
        }

        private void PhotoListLoad(object sender, EventArgs e)
        {
            FillPhotoList();
        }
    }
}
